package com.blb.community.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.community.entity.SysMenu;
import com.blb.community.entity.SysUser;
import com.blb.community.entity.SysUserPost;
import com.blb.community.entity.SysUserRole;
import com.blb.community.entity.vo.SysUserVO;
import com.blb.community.mapper.*;
import com.blb.community.service.ISysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.jar.Pack200;

/**
 * <p>
 * 用户信息表 服务实现类
 * </p>
 *
 * @author 帅帅
 * @since 2022-07-02
 */
@Service
@Transactional
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysPostMapper sysPostMapper;
    @Autowired
    private SysUserPostMapper sysUserPostMapper;
    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @Override
    public List<SysUserVO> getAllUserInfo() {
        return sysUserMapper.getAllUserInfo();
    }

    @Override
    public void updateUserInfo(SysUserVO userVO) {
        sysUserMapper.updateUserInfo(userVO);
        if (userVO.getPostIds().size() != 0) {
            sysUserPostMapper.delete(new QueryWrapper<SysUserPost>().lambda().eq(SysUserPost::getUserId, userVO.getUserId()));
            for (int i = 0; i < userVO.getPostIds().size(); i++) {
                sysUserPostMapper.insert(new SysUserPost(userVO.getUserId(), userVO.getPostIds().get(i)));
            }
        }
        if (userVO.getRoleIds().size() != 0) {
            sysUserRoleMapper.delete(new QueryWrapper<SysUserRole>().lambda().eq(SysUserRole::getUserId, userVO.getUserId()));
            for (int i = 0; i < userVO.getRoleIds().size(); i++) {
                sysUserRoleMapper.insert(new SysUserRole(userVO.getUserId(), userVO.getRoleIds().get(i)));
            }
        }
        sysUserMapper.updateById(userVO);

    }

    @Override
    public List<SysUserVO> getUserlist() {
        return sysUserMapper.getUserList();
    }

    @Override
    public void addList(List<SysUser> list) {
        for (int i = 0; i < list.size(); i++) {
            SysUser sysUser = list.get(i);
            System.out.println(sysUser.getUserName());
        }
        sysUserMapper.add(list);

    }

    @Override
    public IPage<List<SysUserVO>> getUserLimitList(SysUserVO sysUserVO) {
        return sysUserMapper.getUserLimitList(sysUserVO, new Page<SysUserVO>(sysUserVO.getPageCount(), sysUserVO.getPageSize()));
    }


    @Override
    public void addUser(SysUserVO sysUserVO) {
        int insert = sysUserMapper.insert(sysUserVO);
        System.out.println(sysUserVO.getUserId());
        List<Integer> postIds = sysUserVO.getPostIds();
        for (int i = 0; i < postIds.size(); i++) {
            SysUserPost sysUserPost = new SysUserPost();
            sysUserPost.setUserId(sysUserVO.getUserId());
            sysUserPost.setPostId(postIds.get(i));
            sysUserPostMapper.insert(sysUserPost);
        }
        List<Integer> roleIds = sysUserVO.getRoleIds();
        for (int i = 0; i < roleIds.size(); i++) {
            SysUserRole sysUserRole = new SysUserRole();
            Integer integer = roleIds.get(i);
            sysUserRole.setRoleId(integer);
            sysUserRole.setUserId(sysUserVO.getUserId());
            sysUserRoleMapper.insert(sysUserRole);
        }

    }

}
